/**
 * @file debug.c
 * @brief Debugging facilities
 *
 * @section License
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 *
 * Copyright (C) 2010-2021 Oryx Embedded SARL. All rights reserved.
 *
 * This file is part of CycloneBOOT Open.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * @author Oryx Embedded SARL (www.oryx-embedded.com)
 * @version 1.0.1
 **/

//Dependencies
#include "stm32f4xx.h"
#include "stm32f4xx_hal.h"
#include "debug.h"
#include "main.h"
//Variable declaration
static UART_HandleTypeDef huart5;


/**
 * @brief Debug UART initialization
 * @param[in] baudrate UART baudrate
 **/

void debugInit(uint32_t baudrate)

{

	GPIO_InitTypeDef GPIO_InitStruct = { 0 };

		/* USER CODE BEGIN UART5_MspInit 0 */

		/* USER CODE END UART5_MspInit 0 */
		/* UART5 clock enable */
		__HAL_RCC_UART5_CLK_ENABLE();

		__HAL_RCC_GPIOC_CLK_ENABLE();
		__HAL_RCC_GPIOD_CLK_ENABLE();
		/**UART5 GPIO Configuration
		 PC12     ------> UART5_TX
		 PD2     ------> UART5_RX
		 */
		GPIO_InitStruct.Pin = GPIO_PIN_12;
		GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
		GPIO_InitStruct.Pull = GPIO_NOPULL;
		GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
		GPIO_InitStruct.Alternate = GPIO_AF8_UART5;
		HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

		GPIO_InitStruct.Pin = GPIO_PIN_2;
		GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
		GPIO_InitStruct.Pull = GPIO_NOPULL;
		GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
		GPIO_InitStruct.Alternate = GPIO_AF8_UART5;
		HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);

	huart5.Instance = UART5;
	huart5.Init.BaudRate = baudrate;
	huart5.Init.WordLength = UART_WORDLENGTH_8B;
	huart5.Init.StopBits = UART_STOPBITS_1;
	huart5.Init.Parity = UART_PARITY_NONE;
	huart5.Init.Mode = UART_MODE_TX_RX;
	huart5.Init.HwFlowCtl = UART_HWCONTROL_NONE;
	huart5.Init.OverSampling = UART_OVERSAMPLING_16;
	if (HAL_UART_Init(&huart5) != HAL_OK) {
		Error_Handler();
}
}

	/**
	 * @brief Display the contents of an array
	 * @param[in] stream Pointer to a FILE object that identifies an output stream
	 * @param[in] prepend String to prepend to the left of each line
	 * @param[in] data Pointer to the data array
	 * @param[in] length Number of bytes to display
	 **/
#include <inttypes.h>
	void debugDisplayArray(FILE *stream,
	   const char_t *prepend, const void *data, size_t length)
	{
	   uint_t i;

	   for(i = 0; i < length; i++)
	   {
	      //Beginning of a new line?
	      if((i % 16) == 0)
	         fprintf(stream, "%s", prepend);
	      //Display current data byte
	      fprintf(stream, "%02" PRIX8 " ", *((uint8_t *) data + i));
	      //End of current line?
	      if((i % 16) == 15 || i == (length - 1))
	         fprintf(stream, "\r\n");
	   }
	}


	/**
	 * @brief Write character to stream
	 * @param[in] c The character to be written
	 * @param[in] stream Pointer to a FILE object that identifies an output stream
	 * @return On success, the character written is returned. If a writing
	 *   error occurs, EOF is returned
	 **/

	int_t fputc(int_t c, FILE *stream)
	{
	   //Standard output or error output?
	   if(stream == stdout || stream == stderr)
	   {
	      //Character to be written
	      uint8_t ch = c;

	      //Transmit data
	      HAL_UART_Transmit(&huart5, &ch, 1, HAL_MAX_DELAY);

	      //On success, the character written is returned
	      return c;
	   }
	   //Unknown output?
	   else
	   {
	      //If a writing error occurs, EOF is returned
	      return EOF;
	   }
	}

